package com.hailou.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.hailou.base.bean.SinaWeather;
import com.hailou.util.PatternUtil;

/**
 * 新浪天气预报
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class SinaWeatherReportService {

	public static void main(String[] args) {
		System.out.println(new SinaWeatherReportService().getSinaWeatherReport("西安",
				"b"));
	}
	
	String indexString = "";
	
	public String getSinaWeatherReport(String cityName, String dateFlag) {
		String weather = "";
		char flag = dateFlag.toCharArray()[0];
		String flagT = "0";
		switch(flag){
		case 'a':flagT="0";indexString="今天天气\n"; break;
		case 'A':flagT="0";indexString="今天天气\n"; break;
		case 'b':flagT="1";indexString="明天天气\n"; break;
		case 'B':flagT="1";indexString="明天天气\n"; break;
		case 'c':flagT="2";indexString="后天天气\n";  break;
		case 'C':flagT="2";indexString="后天天气\n";  break;
		case 'd':flagT="3";indexString="大后天日天气\n";  break;
		case 'D':flagT="3";indexString="大后天日天气\n";  break;
		case 'e':flagT="4";indexString="第四天天气\n";  break;
		case 'E':flagT="4";indexString="第四天天气\n";  break;
		default: flagT="4";indexString="第四天天气\n"; break; 
		}
		try {
			URL url = new URL("http://php.weather.sina.com.cn/xml.php?city="
					+ URLEncoder.encode(cityName, "gb2312")
					+ "&password=DJOYnieT8234jlsK&day=" + flagT);
			HttpURLConnection huc = (HttpURLConnection) url.openConnection();
			huc.setRequestMethod("GET");
			huc.setDoInput(true);
			weather = extractSinaWeatherBeanStrng(huc.getInputStream());
			url = null;
			huc.disconnect();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return weather;
	}

	public String extractSinaWeatherBeanStrng(InputStream in) {
		SinaWeather sw = new SinaWeather();
		SAXReader sax = new SAXReader();
		try {
			Document doc = sax.read(in);
			Element root = doc.getRootElement();
			Element parentElement = root.element("Weather");
			for (Iterator iterator = parentElement.elementIterator(); iterator
					.hasNext();) {
				Node node = (Node) iterator.next();
				System.out.println(node.getName());
				if (node.getName().equals("city")) {
					sw.setCityName(node.getText());
				} else if (node.getName().equals("status1")) {
					sw.setStatus1(node.getText());
				} else if (node.getName().equals("status2")) {
					sw.setStatus2(node.getText());
				} else if (node.getName().equals("direction1")) {
					sw.setDirection1(node.getText());
				} else if (node.getName().equals("power1")) {
					sw.setPower1(node.getText());
				} else if (node.getName().equals("temperature1")) {
					sw.setTemperature1(node.getText());
				} else if (node.getName().equals("temperature2")) {
					sw.setTemperature2(node.getText());
				} else if (node.getName().equals("chy_shuoming")) {
					sw.setTemperature2(node.getText());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return indexString+sw.toString()+"\n\n";
	}
	
	
	/**
	 * @param cityName
	 * @return
	 */
	public String getCityCode(String cityName) {
		//new WeatherReport().getWeatherReport("101110101");
		Properties properties = new Properties();
		String cityCode = null;
		try {
			InputStream in = WeatherReportService.class.getResourceAsStream("/weatherCityCode.properties");
			properties.load(in);
			cityCode = properties.getProperty(cityName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityCode;
	}
}
